package cn.anosi.repository;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>,JpaSpecificationExecutor<T> {
	/**
	 * 单条查询
	 * @param id
	 * @return
	 */
	@Query("select t from #{#entityName} t where t.isDel = 0 and id = ?")
	public T getById(Serializable id);
	
	/**
	 * 查询所有未删除的数据（分页）
	 * @param pageable
	 * @return
	 */
	@Query("select t from #{#entityName} t where t.isDel = 0")
	public Page<T> list(Pageable pageable);
	
	/**
	 * 列表(不分页)
	 * @param
	 * @return
	 */
	@Query("select t from #{#entityName} t where t.isDel = 0")
	List<T> list();
	
	/**
	 * 锁定/启用
	 * @param id
	 * @param status
	 * @return
	 */
	@Modifying
	@Query("update #{#entityName} t set t.status = ?2 where t.id = ?1")
	int lock(Serializable id, Integer status);
	
	/**
	 * 单条删除
	 * @param id
	 * @return
	 */
	@Modifying
	@Query("update #{#entityName} t set t.isDel = 1 where t.id = ?")
	int deleteOne(Serializable id);

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@Modifying
	@Query("update #{#entityName} t set t.isDel = 1 where t.id in (?1)")
	int batchDel(List ids);
}
